package com.idbk.chargestation.api;

import com.idbk.chargestation.AppContext;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.Map;

public class APIWrapper {

    /**
     * 发送请求并在请求中 添加token信息
     *
     * @param URL      url
     * @param params   参数
     * @param callback 回调
     */
    public static RequestCall postWithToken(
            String URL,
            final Map<String, String> params,
            Callback<?> callback) {
        AppContext appContext = AppContext.getInstance();
        String token = appContext.getToken();
        if (token != null && token.length() > 1) {
            params.put("token", appContext.getToken());
        }

        RequestCall request = OkHttpUtils
                .post()
                .url(URL)
                .params(params)
                .build();
        request.execute(callback);
        return request;
    }


    public static RequestCall post(
            String URL,
            final Map<String, String> params,
            Callback<?> callback) {
        RequestCall request = OkHttpUtils
                .post()
                .url(URL)
                .params(params)
                .build();
        request.execute(callback);
        return request;
    }

    public static RequestCall get(String URL, Callback<?> callback) {
        RequestCall request = OkHttpUtils
                .get()
                .url(URL)
                .build();
        request.execute(callback);
        return request;
    }

}
